#include"message.hpp"

// int main()
// {
//     // DLOG("%s","hello");
//     // DLOG("%d" ,111);

//     // DLOG("%d" ,111);
//     // DLOG("hello");

//     std::cout<<RPC_project::uuid();


//     return 0;
// }

int main()
{
    // RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::REQ_RPC);
    // RPC_project::RpcRequest::ptr rpc_ptr = std::dynamic_pointer_cast<RPC_project::RpcRequest>(base_ptr);
    // Json::Value prams;
    // prams["num1"]=26;
    // prams["num2"]=66;
    // rpc_ptr->SetMethod("Add");
    // rpc_ptr->SetParams(prams);

    // std::string s = rpc_ptr->Serialize();
    // std::cout<<s<<std::endl;

    // RPC_project::BaseMessage::ptr base_ptr1 = RPC_project::MessageFactory::Create(RPC_project::MType::REQ_RPC);
    // RPC_project::RpcRequest::ptr rpc_ptr1 = std::dynamic_pointer_cast<RPC_project::RpcRequest>(base_ptr);
    // bool ret =base_ptr1->Deserialize(s);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret=base_ptr1->Check();
    //     if(ret == false)
    // {
    //     return -1;
    // }
    // std::cout<<rpc_ptr1->Method()<<std::endl;
    // std::cout<<rpc_ptr1->Params()["num1"].asInt()<<std::endl;
    // std::cout<<rpc_ptr1->Params()["num2"].asInt()<<std::endl;



    // RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::REQ_TOPIC);
    // RPC_project::TopicRequest::ptr t_ptr = std::dynamic_pointer_cast<RPC_project::TopicRequest>(base_ptr);

    // t_ptr->SetTopickKey("news");
    // t_ptr->SetOpType(RPC_project::TopicOptype::TOPIC_PUBLISH);
    // t_ptr->SetTopickMsg("今天是7月6日");
    // std::string s = t_ptr->Serialize();
    // std::cout<<s<<std::endl;

    // bool ret =base_ptr->Deserialize(s);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret=base_ptr->Check();
    //     if(ret == false)
    // {
    //     return -1;
    // }
    // std::cout<<t_ptr->TopickKey()<<std::endl;
    // std::cout<<(int)t_ptr->OpType()<<std::endl;
    // std::cout<<t_ptr->TopickMsg()<<std::endl;






    // RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::REQ_SERVICE);
    // RPC_project::ServiceRequest::ptr s_ptr = std::dynamic_pointer_cast<RPC_project::ServiceRequest>(base_ptr);

    // s_ptr->SetMethod("Add");
    // s_ptr->SetOpType(RPC_project::ServiceOptype::SERVICE_REGISTRY);
    // RPC_project::Address addr("127.0.0.1" ,8089);
    // s_ptr->SetHost(addr);

    // std::string s = s_ptr->Serialize();
    // std::cout<<s<<std::endl;

    // bool ret =base_ptr->Deserialize(s);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret=base_ptr->Check();
    //     if(ret == false)
    // {
    //     return -1;
    // }
    // std::cout<<s_ptr->Method()<<std::endl;
    // std::cout<<(int)s_ptr->OpType()<<std::endl;
    // std::cout<<s_ptr->Host().first<<std::endl;
    // std::cout<<s_ptr->Host().second<<std::endl;



    // RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::RSP_RPC);
    // RPC_project::RpcResponse::ptr r_ptr = std::dynamic_pointer_cast<RPC_project::RpcResponse>(base_ptr);

    // r_ptr->SetRcode(RPC_project::RCode::RCODE_OK);
    // r_ptr->SetResult(33);
    // std::string s = r_ptr->Serialize();
    // std::cout<<s<<std::endl;

    // bool ret =base_ptr->Deserialize(s);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret=base_ptr->Check();
    //     if(ret == false)
    // {
    //     return -1;
    // }
    // std::cout<<RPC_project::errReason(r_ptr->Rcode())<<std::endl;
    // std::cout<<r_ptr->Result().asInt()<<std::endl;

    // RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::RSP_TOPIC);
    // RPC_project::TopicResponse::ptr t_ptr = std::dynamic_pointer_cast<RPC_project::TopicResponse>(base_ptr);

    // t_ptr->SetRcode(RPC_project::RCode::RCODE_OK);
    
    // std::string s = t_ptr->Serialize();
    // std::cout<<s<<std::endl;

    // bool ret =base_ptr->Deserialize(s);
    // if(ret == false)
    // {
    //     return -1;
    // }
    // ret=base_ptr->Check();
    //     if(ret == false)
    // {
    //     return -1;
    // }
    // std::cout<<RPC_project::errReason(t_ptr->Rcode())<<std::endl;


    RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::RSP_SERVICE);
    RPC_project::ServiceResponse::ptr s_ptr = std::dynamic_pointer_cast<RPC_project::ServiceResponse>(base_ptr);

    s_ptr->SetRcode(RPC_project::RCode::RCODE_OK);
    s_ptr->SerMethod("Add");
    s_ptr->SetOpType( RPC_project::ServiceOptype::SERVICE_DISCOVERY);
    std::vector< RPC_project::Address> hosts;
    hosts.push_back(RPC_project::Address("127.0.0.1",8089));
    hosts.push_back(RPC_project::Address("127.0.0.2",8090));
    s_ptr->SetHost(hosts);

    std::string s = s_ptr->Serialize();
    std::cout<<s<<std::endl;

    bool ret =base_ptr->Deserialize(s);
    if(ret == false)
    {
        return -1;
    }
    ret=base_ptr->Check();
        if(ret == false)
    {
        return -1;
    }
    std::cout<<RPC_project::errReason(s_ptr->Rcode())<<std::endl;
    std::cout<<(int)s_ptr->OpType()<<std::endl;
    std::cout<<s_ptr->Method()<<std::endl;
    std::vector< RPC_project::Address> host1 =s_ptr->Hosts();
    for(auto &e: host1)
    {
        std::cout<< e.first <<":"<< e.second <<std::endl;
    }

    return 0;
}